package com.pay.job.service.impl;

import com.pay.common.pay.utils.DateUtils;
import com.pay.job.entity.AggregateAmountDO;
import com.pay.job.mapper.PaymentInfoMapper;
import com.pay.job.service.PaymentJobDailyReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author 周益和
 * @ClassName PaymentJobDailyReportServiceImpl
 * @qq 1552951165
 */
@Service
@Slf4j
public class PaymentJobDailyReportServiceImpl implements PaymentJobDailyReportService {
    
    @Autowired
    private PaymentInfoMapper paymentInfoMapper;
    
    /**
     * 分页大小
     */
    private static final Integer PAGE_SIZE = 2;
    
    /**
     * 查询每个商户 昨日交易金额
     * @param shardIndex
     * @return
     */
    @Override
    public List<AggregateAmountDO> merchantTransactionAmount(Integer shardIndex) {
        // 1.获取昨日的时间
        String yesterdayTime = DateUtils.yesterdayTime();
        
        // 2.获取本分片中昨日交易额数据
        List<AggregateAmountDO> aggregateAmountList = paymentInfoMapper.merchantTransactionAmount
                (yesterdayTime, shardIndex * PAGE_SIZE);
        return aggregateAmountList;
    }
    
    /**
     * 多线程异步处理
     * @param aggregateAmountList
     */
    @Override
    @Async("taskExecutor")
    public void asynSendMerchantTransactionAmount(List<AggregateAmountDO> aggregateAmountList) {
        for (AggregateAmountDO aggregateAmount : aggregateAmountList) {
            String merchantId = aggregateAmount.getMerchantId();
            BigDecimal payAmount = aggregateAmount.getPayAmount();
            log.info("merchantId:{},payAmount:{}", merchantId, payAmount);
        }
    }
}
